package com.moon.back.dao;

import com.moon.back.entity.file.FileNode;
import org.beetl.sql.mapper.BaseMapper;
import org.beetl.sql.mapper.annotation.Param;
import org.beetl.sql.mapper.annotation.Sql;

import java.util.List;

public interface FileNodeDao extends BaseMapper<FileNode> {

    @Sql("select sum(`file_size`) from `file_node` where `file_path` like ?")
    Double getFileCapacity(@Param("path") String path );

    @Sql("select count(1) from file_node where `file_path` = ?")
    Integer existFolder(@Param("path") String path );

    @Sql("select from file_node where `file_path` = ? ")
    List<FileNode> getFileNodeList(@Param("path") String path );

    @Sql("select from file_node where `file_path` like ? or `file_name` like ?")
    List<FileNode> seatFileNodeList(@Param("name") String name);


    @Sql("WITH RECURSIVE sub_files AS (\n" +
            "    SELECT file_id, file_parent_id\n" +
            "    FROM file_node\n" +
            "    WHERE file_parent_id = ?  -- 指定要删除的文件夹的 file_id（如 123）\n" +
            "    UNION ALL\n" +
            "    SELECT f.file_id, f.file_parent_id\n" +
            "    FROM file_node f\n" +
            "             JOIN sub_files s ON f.file_parent_id = s.file_id\n" +
            ")\n" +
            "delete from file_node\n" +
            "where file_id in (select file_id from sub_files);")
    Integer findDeleteByParentFileId(Integer fileId);


@Sql("UPDATE file_node" +
        "SET file_path = REPLACE(file_path, ?, ?)" +
        "WHERE file_path LIKE ?;")
    Integer renameByParentFilePath( @Param("oldFileName")String oldFileName,
                                    @Param("newFileName") String newFileName,
                                 @Param("filePathStr") String filePathStr );
//    Integer deleteByParentFilePath(String filePath);




}
